# Module for items that are held in the player's inventory

import imagecache
from field import Field
from map import PlacedObject
import plant

# Uninstantiatable base class.
# Subclasses must implement the fields:
#     image_path: where the image is stored
class InventoryItem:
    def __init__(*kwargs):
        import inspect
        caller = inspect.getouterframes(inspect.currentframe())[1][3]
        raise NotImplementedError(caller + ' must be implemented in subclass')
    
    def get_image(self):
        return imagecache.get_image(self.image_path)
    
    def use(self, game):
        pass


# A watering can. Used to water plots.
class WateringCan(InventoryItem):
    def __init__(self):
        self.image_path = 'wateringcan'
    
    # waters the tile that the player is on
    def use(self, game):
        for i in game.map.get_items(game.player.get_front()):
            if isinstance(i, PlacedObject) and isinstance(i.object, Field):
                i.object.set_watered(i.get_relative_position(game.player.get_front()))
                game.map.changed = True
        
    def __eq__(self, object):
        return isinstance(object, WateringCan)

# Some seeds. Used for planting plants.
class Seeds(InventoryItem):
    def __init__(self, plant_name):
        self.plant_name = plant_name
        self.image_path = self.plant_name + '-seeds'
    
    # plants these seeds
    def use(self, game):
        for i in game.map.get_items(game.player.get_front()):
            if isinstance(i, PlacedObject) and isinstance(i.object, Field):
                if i.object.set_plant(i.get_relative_position(game.player.get_front()), plant.Plant_from_dict(game.plant_dict[self.plant_name])):
                    game.player.inventory.remove()
                    game.map.changed = True
    
    def __eq__(self, object):
        return (isinstance(object, Seeds) and self.plant_name == object.plant_name and self.image_path == object.image_path)